/*
 * @Author: zxc
 * @Date: 2020-08-04 19:26:30
 * @LastEditTime: 2020-08-04 20:26:38
 * @LastEditors: zxc
 * @Description: 集合
 */
class Set {
  constructor() {
    this.items = {};
  }
  has(e) {
    return Reflect.has(this.items, e);
  }
  add(e) {
    if (!this.has(e)) {
      this.items[e] = e;
      return true;
    }
    return false;
  }
  delete(e) {
    if (this.has(e)) {
      Reflect.deleteProperty(this.items, e);
      return true;
    }
    return false;
  }
  values() {
    return Object.values(this.items);
  }
  size() {
    return Object.keys(this.items).length;
  }
  clear() {
    this.items = {};
  }
  getitems(){
    return this.items
  }
  // 并集
  union(s) {
    if(this.size()<1) throw new Error('你不要过来啊')
    const unionS = new Set();
    this.values().forEach((e) => unionS.add(e));
    s.values().forEach((e) => unionS.add(e));
    return unionS
  }
  // 交集
  intersection(s) {
    if(this.size()<1) throw new Error('你不要过来啊')
    const interS = new Set();
    this.values().forEach((e) => {
      if (Reflect.has(s.getitems(), e)) {
        interS.add(e);
      }
    });
    return interS
  }
  // 差集
  difference(s){
    if(this.size()<1) throw new Error('你不要过来啊')
    const diffS = new Set()
    this.values().forEach(e => {
      if(!Reflect.has(s.getitems(),e)){
        diffS.add(e)
      }
    })
    return diffS
  }
  // 子集
  isSubsetOf(s){
    if(this.size()<1 || s.size()<1){
      throw new Error('麻烦整点阳间的东西')
    }
    if(this.size() > s.size()){
      throw new Error('你在让爷爷叫爸爸？')
    }
    let isSubset = this.values().every(e => Reflect.has(s.getitems(),e))
    return isSubset
  }
}
let s = new Set();
s.add("a");
s.add("b");
s.add("c");


let ss = new Set()
ss.add('a')
ss.add('b')

let sss = new Set()
sss.add('d')

console.log(s.union(sss))
console.log(s.intersection(ss))
console.log(s.difference(ss))
console.log(sss.isSubsetOf(s))